package com.qfedu.inspectionReception.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.inspectionReception.dto.InspectionReceptionDTO;
import com.qfedu.inspectionReception.dto.InspectionReceptionStateDto;
import com.qfedu.inspectionReception.mapper.InspectionReceptionMapper;
import com.qfedu.inspectionReception.pojo.InspectionReception;
import com.qfedu.inspectionReception.service.InspectionReceptionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class InspectionReceptionServiceImpl extends ServiceImpl<InspectionReceptionMapper, InspectionReception> implements InspectionReceptionService {

    @Transactional(readOnly = true)
    @Override
    public Page<InspectionReception> getInspectionReceptions(InspectionReceptionDTO inspectionReceptionDTO) {
        LambdaQueryWrapper<InspectionReception> wrapper = new LambdaQueryWrapper<>();
        Long id = inspectionReceptionDTO.getId();
        if (id != null) {
            wrapper.eq(InspectionReception::getId, id);
        }

        Long inspectionId = inspectionReceptionDTO.getInspectionId();
        if (inspectionId != null) {
            wrapper.eq(InspectionReception::getInspectionId, inspectionId);
        }

        String operationUser = inspectionReceptionDTO.getOperationUser();
        if (StringUtils.hasLength(operationUser)) {
            wrapper.like(InspectionReception::getOperationUser, operationUser);
        }
        Page<InspectionReception> page = new Page<>(inspectionReceptionDTO.getCurrentPage(), inspectionReceptionDTO.getPageSize());
        return page(page, wrapper);
    }

    @Override
    public boolean saveInspectionReception(InspectionReception inspectionReception) {
        //设置默认值
        //1. 生成唯一的setInspectionId
        UUID uuid = UUID.randomUUID();
        long mostSignificantBits = uuid.getMostSignificantBits();
        long leastSignificantBits = uuid.getLeastSignificantBits();
        inspectionReception.setInspectionId(Math.abs(mostSignificantBits ^ leastSignificantBits));
        //默认进度0%
        inspectionReception.setRate("0%");
        //默认状态未提交
        inspectionReception.setState(0);
        //先查找是否由重复
        LambdaQueryWrapper<InspectionReception> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(String.valueOf(inspectionReception.getId())), InspectionReception::getId, inspectionReception.getId())
                .or()
                .eq(StringUtils.hasLength(String.valueOf(inspectionReception.getInspectionId())), InspectionReception::getInspectionId, inspectionReception.getInspectionId());
        List<InspectionReception> inspectionReceptions = this.baseMapper.selectList(queryWrapper);
        if (inspectionReceptions.size() > 0) {
            return false;
        }
        return baseMapper.insert(inspectionReception) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeUseState(InspectionReceptionStateDto inspectionReceptionStateDto) {
        LambdaUpdateWrapper<InspectionReception> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(InspectionReception::getState, inspectionReceptionStateDto.getState());
        wrapper.eq(InspectionReception::getId, inspectionReceptionStateDto.getId());
        return update(wrapper);
    }
}
